// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var router = require("../hash/pageRouter");
    var constant = require("constant");
    var css_detail = require("./accountRecord.css");
    var databind = require("databind");
    var pageIndex = 1,
		pageSize = 8,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
    
    var init = function(){
    	router.init();
    	inquireFinanceApplicationList();
		initEvent();
    };
    
	var initEvent = function(){
		if(base.getData("sid")==null){
			location.href="../login/login.html";
		}
		
		$("#accountRecordSearchButton").unbind("click").click(function(){
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化				
			inquireFinanceApplicationList();
		});
    };
    
    var inquireFinanceApplicationDetail = function(){
		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			async: false,
			oData: {
                financeId: financeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	var tpl = $("#accountDetailTpl").html();
                	var html = juicer(tpl, data);
                	$("#accountDetailBody").html(html);
                } else {
//                  base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
	//企业查询融资信息列表
    var inquireFinanceApplicationList = function(){
    	if($("#financeState").val()!=-1){
    		var financeState = $("#accountRecord #accountRecordFinanceState").val();
    	}
    	var dateBegin = $("#dateBegin").val();
		var dateEnd = $("#dateEnd").val();
		var financeNumber = $("#accountRecord .financeNumber").val();
		var bulkstockName = $("#accountRecord .bulkstockName").val();
		var waybillNumber = $("#accountRecord .waybillNumber").val();
		var caseNumber = $("#accountRecord .caseNumber").val();
		base.fnAjax({
			action: "inquireFinanceApplicationList.json",
			async: false,
			oData: {
                pageSize: pageSize,
           		pageIndex: pageIndex,
           		financeState: financeState,
           		dateBegin:dateBegin,
           		dateEnd:dateEnd,
           		financeNumber:financeNumber,
           		bulkstockName:bulkstockName,
           		waybillNumber: waybillNumber,
           		caseNumber: caseNumber
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	var tpl = $("#accountRecordListTpl").html();
                	var html = juicer(tpl, data);
                	$("#accountRecordListBody").html(html);
                	$(".cancelApplication").unbind("click").click(function(){
                	});
                	
                	$(".accountRecordDetail").unbind("click").click(function(){
                		router.route("account/accountDetail.html?financeId="+$(this).parents("tr").attr("financeId"));
                	});
                	//提货详情
        			$("#accountRecord .details").unbind("click").click(function(){
        				var productType = $(this).parents("tr").attr("productType");
        				if(productType == 6) { //代销提货
        					router.route("account/createPickupDetail.html?pickupId=" + $(this).parents("tr").attr("financeId"));
        				}else if(productType == 5) { //代采提货
        					router.route("account/createPickupDetail1.html?pickupId=" + $(this).parents("tr").attr("financeId"));
        				}
        			});
                	totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
                	base.pageControl($("#accountRecord .pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireFinanceApplicationList();
						});
                } else {
//                  base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
    return {
        init:init
    };
});